package com.example.demo.controller;

import com.example.demo.mqtt.MqttPushClient;
import org.eclipse.paho.client.mqttv3.MqttException;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("mqtt")
public class MqttController {
    /**
     * mqtt订阅
     * @param topic
     *
     * url: http://localhost:8080/mqtt/subscribe?topic=test
     */
    @RequestMapping("subscribe")
    public void subscribe(String topic) {
//        订阅主题
        MqttPushClient.getInstance().subscribe(topic);
    }

    /**
     * mqtt推送
     * @param topic
     * @param data
     *
     * url: http://localhost:8080/mqtt/publish?topic=test&data=test
     */
    @RequestMapping("publish")
    public void publish(String topic,String data) {
//        推送
        MqttPushClient.getInstance().publish(topic,data);
    }
}
